package com.yanggeng.demo.beginnerguidedemo;

import android.app.Application;
import android.graphics.Typeface;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.model.LatLng;
import com.yanggeng.demo.beginnerguidedemo.utils.LocationUtils;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

/**
 * Created by yanggeng on 2017/6/30.
 */

public class TestApplication extends Application implements AMapLocationListener {

    private static TestApplication application;

    //声明mLocationOption对象
    private AMapLocationClientOption mLocationOption;
    private AMapLocationClient mlocationClient;
    private LatLng currLatLng;
    private String address;
    private String city;
    public static Typeface TYPEFACE_NUMBER;

    public static TestApplication getApplication() {
        return application;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        application = this;
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
//                .addInterceptor(new LoggerInterceptor("TAG"))
                .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                .readTimeout(10000L, TimeUnit.MILLISECONDS)
                //其他配置
                .build();

        OkHttpUtils.initClient(okHttpClient);

        mlocationClient = new AMapLocationClient(this);
        //初始化定位参数/
        mLocationOption = new AMapLocationClientOption();
        //设置定位监听
        mlocationClient.setLocationListener(this);
        //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
        //设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setInterval(200000);
        //设置定位参数
        mlocationClient.setLocationOption(mLocationOption);
        // 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
        // 注意设置合适的定位时间的间隔（最小间隔支持为2000ms），并且在合适时间调用stopLocation()方法来取消定位请求
        // 在定位结束后，在合适的生命周期调用onDestroy()方法
        // 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除
        //启动定位
        mlocationClient.startLocation();

        setTypeface();
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (aMapLocation != null) {
            if (aMapLocation.getErrorCode() == 0) {
                currLatLng = LocationUtils.getLocationLatLng(aMapLocation);
                address = aMapLocation.getAddress();
                city = aMapLocation.getCity();
            } else {
                String errText = "定位失败," + aMapLocation.getErrorCode() + ": " + aMapLocation.getErrorInfo();
                Log.e("AmapErr", errText);
            }
        }
    }

    public LatLng getCurrLatLng() {
        return currLatLng;
    }

    public String getAddress() {
        return address;
    }

    public String getCity() {
        return city;
    }

    private void setTypeface() {
        TYPEFACE_NUMBER = Typeface.createFromAsset(getAssets(), "fonts/DINCond-Bold.otf");
    }
}
